﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Rob Ashton 2009
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@mvcex.com
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Web.Mvc;
using System.Web.Hosting;
using System.IO;
using System.Web;
using System.Threading;
using Moq;
using MvcEx.Views;

namespace MvcEx.Tests
{
    [TestFixture]
    public class MvcExViewEngineTests
    {
        [Test]
        public void create_view_engine()
        {
            var testConfiguration = new MvcExViewEngineConfiguration("data/MvcExViewEngineTests/site");
            testConfiguration.RegisterComponent("moduleOne");
            testConfiguration.RegisterComponent("moduleTwo");

            Mock<IMvcExViewEngineConfigurationSelector> providerMock = new Mock<IMvcExViewEngineConfigurationSelector>();
            providerMock.Setup(x => x.GetConfiguration()).Returns(
                testConfiguration);

            Mock<ControllerBase> controllerMock = new Mock<ControllerBase>();
            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();

            var routeData = new System.Web.Routing.RouteData();
            routeData.Values["controller"] = "home";

            ControllerContext context = new ControllerContext(
                new System.Web.Routing.RequestContext(
                    contextMock.Object, routeData),
                     controllerMock.Object);


            MvcExViewEngine engine = new MvcExViewEngine(providerMock.Object);
        }

        [Test]
        public void request_partial_view()
        {
            var testConfiguration = new MvcExViewEngineConfiguration("~/site");
            testConfiguration.RegisterComponent("moduleOne");
            testConfiguration.RegisterComponent("moduleTwo");

            Mock<IMvcExViewEngineConfigurationSelector> providerMock = new Mock<IMvcExViewEngineConfigurationSelector>();
            providerMock.Setup(x => x.GetConfiguration()).Returns(
                testConfiguration);

            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            Mock<HttpServerUtilityBase> serverMock = new Mock<HttpServerUtilityBase>();
            Mock<ControllerBase> controllerMock = new Mock<ControllerBase>();

            serverMock.Setup(x => x.MapPath(It.IsAny<String>()))
                .Returns((String arg) => TestHelper.MapPath("MvcExViewEngineTests", arg));

            contextMock.SetupGet(x => x.Server).Returns(serverMock.Object);

            var routeData = new System.Web.Routing.RouteData();
            routeData.Values["controller"] = "home";

            ControllerContext context = new ControllerContext(
                new System.Web.Routing.RequestContext(
                    contextMock.Object, routeData),
                    controllerMock.Object);

            MvcExViewEngine engine = new MvcExViewEngine(providerMock.Object);

            var view = engine.FindPartialView(
                context,
                "SomePartialView",
                false);

            Assert.NotNull(view);
            Assert.NotNull(view.View);
        }

        [Test]
        public void request_full_view()
        {
            var testConfiguration = new MvcExViewEngineConfiguration("~/site");
            testConfiguration.RegisterComponent("moduleOne");
            testConfiguration.RegisterComponent("moduleTwo");

            Mock<IMvcExViewEngineConfigurationSelector> providerMock = new Mock<IMvcExViewEngineConfigurationSelector>();
            providerMock.Setup(x => x.GetConfiguration()).Returns(
                testConfiguration);

            Mock<ControllerBase> controllerMock = new Mock<ControllerBase>();

            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            Mock<HttpServerUtilityBase> serverMock = new Mock<HttpServerUtilityBase>();

            serverMock.Setup(x => x.MapPath(It.IsAny<String>()))
                .Returns((String arg) => TestHelper.MapPath("MvcExViewEngineTests", arg));

            contextMock.SetupGet(x => x.Server).Returns(serverMock.Object);

            var routeData = new System.Web.Routing.RouteData();
            routeData.Values["controller"] = "home";

            ControllerContext context = new ControllerContext(
                new System.Web.Routing.RequestContext(
                    contextMock.Object, routeData),
                    controllerMock.Object);

            MvcExViewEngine engine = new MvcExViewEngine(providerMock.Object);

            var view = engine.FindView(
                context,
                "Index",
                "",
                false);

            Assert.NotNull(view);
            Assert.NotNull(view.View);
        }

        [Test]
        public void release_view()
        {
            var testConfiguration = new MvcExViewEngineConfiguration("~/site");
            testConfiguration.RegisterComponent("moduleOne");
            testConfiguration.RegisterComponent("moduleTwo");

            Mock<IMvcExViewEngineConfigurationSelector> providerMock = new Mock<IMvcExViewEngineConfigurationSelector>();
            providerMock.Setup(x => x.GetConfiguration()).Returns(
                testConfiguration);

            Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
            Mock<ControllerBase> controllerMock = new Mock<ControllerBase>();

            var routeData = new System.Web.Routing.RouteData();
            routeData.Values["controller"] = "home";

            ControllerContext context = new ControllerContext(
                new System.Web.Routing.RequestContext(
                    contextMock.Object, routeData),
                    controllerMock.Object);


            MvcExViewEngine engine = new MvcExViewEngine(providerMock.Object);

            // Not that this actually does anything
            engine.ReleaseView(
               context,
               new WebFormView("data/MvcExViewEngineTests/site/moduleOne/home/index.aspx"));

        }
        
    }
}
